"""
Case Type   : ustore
Case Name   : 初始化环境
Create At   : 2024.8.19
Owner       : wang-tianjing1
Description :
    1、清理回收信息; expect:成功
    2、设置enable_recyclebin为默认值
    3、设置enable_default_ustore_table为默认值
    4、设置undo_retention_time为默认值
    5、设置recyclebin_retention_time为默认值
    6、设置vacuum_defer_cleanup_age为默认值
    7、设置version_retention_age为默认值
    8、重启数据库
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
    5、成功
    6、成功
    7、成功
    8、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class UstoreTest(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        status = self.sh_primary.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)

    def test_show(self):
        text = '----step1:清理回收信息; expect:成功----'
        self.log.info(text)
        sql = f"purge recyclebin;"
        result = self.sh_primary.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("PURGE RECYCLEBIN", result, "执行失败" + text)

        text = '----step2:设置enable_recyclebin为默认值;expect:成功----'
        self.log.info(text)
        guc1 = self.sh_primary.execute_gsguc('reload',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             "enable_recyclebin")
        self.log.info(guc1)
        self.assertTrue(guc1, '执行失败:' + text)

        text = '----step3:设置enable_default_ustore_table为默认值;expect:成功----'
        self.log.info(text)
        guc1 = self.sh_primary.execute_gsguc('reload',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             "enable_default_ustore_table")
        self.log.info(guc1)
        self.assertTrue(guc1, '执行失败:' + text)

        text = '----step4:设置undo_retention_time为默认值;expect:成功----'
        self.log.info(text)
        guc1 = self.sh_primary.execute_gsguc('reload',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             "undo_retention_time")
        self.log.info(guc1)
        self.assertTrue(guc1, '执行失败:' + text)

        text = '----step5:设置recyclebin_retention_time为默认值;expect:成功----'
        self.log.info(text)
        guc1 = self.sh_primary.execute_gsguc('reload',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             "recyclebin_retention_time")
        self.log.info(guc1)
        self.assertTrue(guc1, '执行失败:' + text)

        text = '----step6:设置vacuum_defer_cleanup_age为默认值;expect:成功----'
        self.log.info(text)
        guc2 = self.sh_primary.execute_gsguc('reload',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             "vacuum_defer_cleanup_age")
        self.log.info(guc2)
        self.assertTrue(guc2, '执行失败:' + text)

        text = '----step7:设置version_retention_age为默认值;expect:成功----'
        self.log.info(text)
        guc3 = self.sh_primary.execute_gsguc('reload',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             "version_retention_age")
        self.log.info(guc3)
        self.assertTrue(guc3, '执行失败:' + text)

        text = '----step8:重启数据库;expect:成功----'
        self.log.info(text)
        restart = self.sh_primary.restart_db_cluster()
        self.log.info(restart)

    def tearDown(self):
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
